package com.example.win.android_banduyujia.peopleactivity.activity.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.entity.LoginInfEntity;
import com.example.win.android_banduyujia.login.application.LoginApplication;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;
import com.example.win.android_banduyujia.ui.view.CircleImageView;
import com.example.win.android_banduyujia.ui.view.ImageLoader;

import java.io.File;

/**
 * Created by Win on 2015/8/11.
 */
public class PhotoMyActivity extends BaseTitleBarActivity {

    private static final String CircleImageView = null;

    private CircleImageView mHeaderImageView;
    private View mContentView;

    private EditText mEditName, mEditFirst, mEditYoga;
    private Button mBtnNext;

    private String tp = null;
    private String mAccessToken;
    private LoginInfEntity  loginInfo;
  //  private LoginApplication mLoginApplication;

    // 关于请求用户头像的变量
    private View v;
    private final int REFRESH_ICON = 1;
    private Bitmap bitmapIcon = null;
    // 刷新头像
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFRESH_ICON:

                    mHeaderImageView.setImageBitmap(bitmapIcon);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_data);

        InitTitles();
        InitViews();
    }

    private void InitTitles() {
        setTitle(R.string.title_text_view_data);
        showLeftBackButton();
    }

    private void InitViews() {
        mHeaderImageView = (CircleImageView) findViewById(R.id.layout_image_view_data);
        mEditName = (EditText) findViewById(R.id.login_layout_edit_text_real_name);
        mEditFirst = (EditText) findViewById(R.id.login_layout_edit_text_first);
        mEditYoga = (EditText) findViewById(R.id.login_layout_edit_text_yoga);
        mBtnNext = (Button) findViewById(R.id.layout_button_data_next);

        mHeaderImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PopupWindows(PhotoMyActivity.this, mHeaderImageView);
            }
        });
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoNextActivity(); // 下一步
            }
        });

       /* // 头像问题
        Bitmap  bit = ImageLoader.getInstance().getImage(loginInfo.getAvatarUrl());
        if(bit != null){
            mHeaderImageView.setImageBitmap(bit);
        }else{
            ImageLoader.getInstance().downloadImage(loginInfo.getAvatarUrl(),mHeaderImageView );
        }*/
    }

    private void gotoNextActivity() {
        startActivity(new Intent(this, WelcomeActivity.class));
    }

    public class PopupWindows extends PopupWindow {

        public PopupWindows(Context mContext, View parent) {
            View view = View.inflate(mContext,R.layout.title_pic_item,null);
            view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins));
            LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
            ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2));

            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();

            Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
            Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
            Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);
            bt1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    photo();
                    dismiss();
                }
            });
            bt2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    picture();
                    dismiss();
                }
            });
            bt3.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }

    private static final int TAKE_PICTURE = 0x000000;
    private static final int TAKE_PHOTO = 0x000001;
    private static final int TAKE_CROP = 0x000002;

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
    }

    public void photo() {
        String  status = Environment.getExternalStorageState();
        if(status.equals(Environment.MEDIA_MOUNTED)){
            File dir = new File(Environment.getExternalStorageDirectory() + "/myimage/");
            if(!dir.exists())dir.mkdirs();

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // 下面这句指定调用相机拍照后的照片存储的路径
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), ".jpg")));
           /* File file = new File(dir, String.valueOf(System.currentTimeMillis()) + ".jpg");
          //  path = file.getPath();
            Uri  imageUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);*/
            intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
            startActivityForResult(intent, TAKE_PICTURE);
        }else{
            Toast.makeText(getApplicationContext(), "没有卡", Toast.LENGTH_LONG).show();
        }
    }

    public void picture() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, TAKE_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case TAKE_PHOTO: // 如果是直接从相册获取
                startPhotoZoom(data.getData());
                break;
            case TAKE_PICTURE: // 如果是调用相机拍照时
                File temp = new File(Environment.getExternalStorageDirectory() + "/.jpg");
                startPhotoZoom(Uri.fromFile(temp));
                break;
            case TAKE_CROP: // 取得裁剪后的图片
                if (data != null) {
                    setPicToView(data);
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    // 裁剪图片方法实现 173 * 174
    public void startPhotoZoom(Uri uri) {
        Intent intent=new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");

        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, TAKE_CROP);
    }
    //	保存裁剪之后的图片数据 201 * 202
    public void setPicToView(Intent data) {
        Bundle  exreas = data.getExtras();
        if(exreas !=null){
            Bitmap  photo = exreas.getParcelable("data");
            Drawable drawable = new BitmapDrawable(photo);

            /* 下面注释的方法是将裁剪之后的图片
		                   以Base64Coder的字符方式上 传到服务器，
		       QQ头像上传采用的方法跟这个类似 213
		    */
            mHeaderImageView.setImageBitmap(photo);
        }
    }
}